/**
 * JAVACC DEMO 1.0
 * @copy right zbxsoft company All rights reserved. 
 * @Package com.zbxsoft.platform.template.helper  
 */
package com.apache.info.template.helper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.apache.api.vo.ParamsVo;
import com.apache.info.entity.PubMetadata;
import com.apache.info.entity.PubSortInfo;
import com.apache.info.manager.PubMetadataManager;
import com.apache.info.manager.PubSortInfoManager;
import com.apache.info.template.FreemarkerHelper;
import com.apache.info.util.FileOperate;
import com.apache.uct.common.ToolsUtil;

/**
 * description:  生成其他实现类
 * @author dyhou 创建时间：2016-9-30  
 * @Copyright(c)2014:北京中百信软件技术有限公司
 */
public class OtherTempManager extends SupperTemplateManager {

	public OtherTempManager(PubSortInfoManager sortInfoManager, PubMetadataManager metadataManager) {
		this.init(sortInfoManager, metadataManager);
	}

	public boolean createOther(Map<String, Object> param) {
		boolean mark = false;
		List<PubSortInfo> list = (List<PubSortInfo>) param.get("sortList");
		String jarPath = String.valueOf(param.get("packPath"));
		String prefix = String.valueOf(param.get("projectName")) + "/src/main/";
		String xmlPath = String.valueOf(param.get("filePath")) + "/" + prefix + "java/" + jarPath.replace(".", "/");
		//		String sysName = jarPath.substring(jarPath.lastIndexOf(".") + 1);//com.zbxsoft.utr中的utr
		String sysName = String.valueOf(param.get("sysCode"));

		try {
			createManager(list, jarPath, prefix, xmlPath, sysName);
			createOther(list, jarPath, prefix, xmlPath, sysName);
			mark = true;
		} catch (Exception e) {
			log.error("生成其他文件失败", e);
		}
		return mark;
	}

	private void createManager(List<PubSortInfo> list, String jarPath, String projectName, String filePath,
			String sysName) throws Exception {
		FileOperate.getInstance().newCreateFolder(filePath + "/manager/");
		FileOperate.getInstance().newCreateFolder(filePath + "/service/impl/");
		FileOperate.getInstance().newCreateFolder(filePath + "/dao/");
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("packPath", jarPath);
		data.put("reqName", sysName);
		data.put("sysName", FileOperate.getInstance().toUpperFirst(sysName));

		ParamsVo vo = new ParamsVo();
		PubMetadata metadata = new PubMetadata();
		for (PubSortInfo map : list) {
			String objName = map.getObjName();
			data.put("sort", map);
			String result = FreemarkerHelper.instance().getTemplateStr("dao/ObjNameDaoImpl.inc", data);
			FileOperate.getInstance().newCreateFile(filePath + "/dao/" + objName + "DaoImpl.java", result);

			result = FreemarkerHelper.instance().getTemplateStr("manager/ObjNameManager.inc", data);
			FileOperate.getInstance().newCreateFile(filePath + "/manager/" + objName + "Manager.java", result);

			metadata.setSortId(String.valueOf(map.getSortId()));
			vo.setObj(metadata);
			List mts = (List) metadataManager.getList(vo);
			if (!ToolsUtil.isEmpty(mts)) {
				for (int i = 0; i < mts.size(); i++) {
					Map<String, Object> mt = (Map) mts.get(i);
					if ("1".equals(String.valueOf(mt.get("dataRestrict")))) {
						data.put("PrimaryAttr",
								FileOperate.getInstance().toUpperFirst(String.valueOf(mt.get("dataAttr"))));
						data.put("PrimaryId", String.valueOf(mt.get("dataAttr")));
						break;
					}
				}
				data.put("metadatas", mts);
			}
			data.put("beanName", FileOperate.getInstance().toLowerFirst(String.valueOf(objName)));
			result = FreemarkerHelper.instance().getTemplateStr("service/impl/ObjNameManagerImpl.inc", data);
			FileOperate.getInstance().newCreateFile(filePath + "/service/impl/" + objName + "ManagerImpl.java", result);
		}
	}

	private void createOther(List<PubSortInfo> list, String jarPath, String projectName, String filePath, String sysName)
			throws Exception {
		String xmlPath = filePath + "/service/";
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("packPath", jarPath);
		data.put("sysName", FileOperate.getInstance().toUpperFirst(sysName));
		String result = FreemarkerHelper.instance().getTemplateStr("service/ServiceImpl.inc", data);
		FileOperate.getInstance().newCreateFile(
				xmlPath + FileOperate.getInstance().toUpperFirst(sysName) + "ServiceImpl.java", result);

		result = FreemarkerHelper.instance().getTemplateStr("manager/UnityBaseManager.inc", data);
		FileOperate.getInstance().newCreateFile(filePath + "/manager/" + "UnityBaseManager.java", result);

		result = FreemarkerHelper.instance().getTemplateStr("service/impl/ProxyManagerImpl.inc", data);
		FileOperate.getInstance()
				.newCreateFile(
						filePath + "/service/impl/" + FileOperate.getInstance().toUpperFirst(sysName)
								+ "ProxyManagerImpl.java", result);
	}
}
